package com.atguigu.gmall.pms.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import javax.annotation.PostConstruct;

/**
 * @description:
 * @author: xie.zh
 * @create: 2021.03.19   14:24
 * @version: 1.0
 */
@Configuration
@Slf4j
public class RabbitConfig {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        //设置消息是否到达交换机 不管消息是否到达都会调用
        this.rabbitTemplate.setConfirmCallback((@Nullable CorrelationData correlationData, boolean ack, @Nullable String cause) -> {
            if (!ack) {
                log.error("消息没有到达交换机：{}", cause);
            }
        });
        //设置消息是否到达队列 只有未到达队列才会调用
        this.rabbitTemplate.setReturnCallback((Message message, int replyCode, String replyText, String exchange, String routingKey) -> {
            log.error("消息未到达队列，交换机:{},路由键:{},消息内容:{}", exchange, routingKey, new String(message.getBody()));
        });
    }
    /*
     *//**
     * 死信队列
     *
     * @return
     *//*
    @Bean
    public Queue deadQueue() {
        return new Queue("SPRING_DEAD_QUEUE", true, false, false);
    }

    *//**
     * 死信交换机
     *
     * @return
     *//*
    @Bean
    public Exchange deadExchange() {
        return new TopicExchange("SPRING_DEAD_EXCHANGE", true, false);
    }

    *//**
     * 绑定死信队列到死信交换机
     *
     * @param deadExchange
     * @param deadQueue
     * @return
     *//*
    @Bean
    public Binding deadBinding(Exchange deadExchange, Queue deadQueue) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("msg.dead").noargs();
    }

    *//**
     * 业务交换机 SPRING_TEST_EXCHANGE
     *//*

     *//**
     * 业务队列
     *//*
    @Bean
    public Queue queue() {
        return QueueBuilder.durable("SPRING_TEST_QUEUE2").withArgument("x-dead-letter-exchange",
                "SPRING_DEAD_EXCHANGE").withArgument("x-dead-letter-routing-key", "msg.dead").build();
    }

    *//**
     * 绑定业务队列到业务交换机
     *
     * @return
     *//*
    @Bean
    public Binding binding() {
        return new Binding("SPRING_TEST_QUEUE2", Binding.DestinationType.QUEUE,
                "SPRING_TEST_EXCHANGE", "msg.test", null);
    }

    *//**
     * 延时队列
     *
     * @return
     *//*
    @Bean
    public Queue ttlQueue() {
        return QueueBuilder.durable("SPRING_TTL_QUEUE")
                .withArgument("x-message-ttl", 60000)
                .withArgument("x-dead-letter-exchange", "SPRING_DEAD_EXCHANGE")
                .withArgument("x-dead-letter-routing-key", "msg.dead")
                .build();
    }

    *//**
     * 绑定延时队列到业务交换机
     *
     * @return
     *//*
    @Bean
    public Binding ttlBinding() {
        return new Binding("SPRING_TTL_QUEUE", Binding.DestinationType.QUEUE
                , "SPRING_TEST_EXCHANGE"
                , "msg.ttl", null);
    }*/
}
